const BASE_URL = 'http://localhost:3000';

export default function Ajax({
    method = 'GET',
    url,
    data={},
}){
    return new Promise((resolve) =>{
        // 通过promise返回异步请求
        const xhr =new XMLHttpRequest();
        xhr.open(method,BASE_URL + url);//配置请求
        xhr.onload=function(){//回调函数
            resolve(JSON.parse(xhr.response));//处理响应
        };
        xhr.onerror=function(){
            if(xhr.status==0){
                console.log('请求失败');
            }
        };
        xhr.send(JSON.stringify(data));//发送请求
    })
}

export async function getBannerList(){
    const result=Ajax({
        url:`/homepage/block/page`,
    });
    return result;
}

export async function getRecommendList(musicd){
    const result=Ajax({
        url:`/playlist/detail?id=${musicd}`, 
    });
    return result;
}

// 获取音乐的播放地址
export async function getAudioSrc(musicId){
    let result=`https://music.163.com/song/media/outer/url?id=${musicId}`;
    return result;
}
// 获取歌曲的信息
export async function getAudioInfo(musicId){
    const result=Ajax({
        url:`/song/detail?ids=${musicId}`,
    });
    return result;
}

// 获取歌词
export async function getAudioLyric(musicId){
    const result=Ajax({
        url:`/lyric?id=${musicId}`,
    })
    return result;
}